//
//  JHWTabBar.m
//  JHWAssistant
//
//  Created by Magician on 2017/3/2.
//  Copyright © 2017年 jugame. All rights reserved.
//

#import "YGTabBar.h"
#import <objc/runtime.h>
#import "UIView+HHLayout.h"
#define topMargin  25
#define CenterBtnW 64


@interface YGTabBar ()

@property (nonatomic, strong) UIButton *centerBtn;

@end

@implementation YGTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {
        _centerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_centerBtn setBackgroundImage:[UIImage imageNamed:@"home_bug_nutritionaladvice_icon"] forState:UIControlStateNormal];
        [_centerBtn setBackgroundImage:[UIImage imageNamed:@"home_bug_nutritionaladvice_icon"] forState:UIControlStateSelected];
        [_centerBtn addTarget:self action:@selector(handleCenterBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_centerBtn];
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    //系统自带的按钮类型是UITabBarButton，找出这些类型的按钮，然后重新排布位置，空出中间的位置
    self.centerBtn.size = CGSizeMake(CenterBtnW, CenterBtnW);
    self.centerBtn.centerX = self.centerX;
    self.centerBtn.y = - topMargin;
}

- (void)handleCenterBtnEvent:(UIButton *)sender {
    
    if (sender.selected) {
        return;
    }
    
    sender.selected = YES;
    
    if ([self.myDelegate respondsToSelector:@selector(tabBarCenterBtnDidClick:)]) {
        [self.myDelegate tabBarCenterBtnDidClick:self];
    }
}

//重写hitTest方法，去监听发布按钮的点击，目的是为了让凸出的部分点击也有反应
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    
    //这一个判断是关键，不判断的话push到其他页面，点击发布按钮的位置也是会有反应的，这样就不好了
    //self.isHidden == NO 说明当前页面是有tabbar的，那么肯定是在导航控制器的根控制器页面
    //在导航控制器根控制器页面，那么我们就需要判断手指点击的位置是否在发布按钮身上
    //是的话让发布按钮自己处理点击事件，不是的话让系统去处理点击事件就可以了
    if (self.isHidden == NO) {
        
        //将当前tabbar的触摸点转换坐标系，转换到发布按钮的身上，生成一个新的点
        CGPoint newP = [self convertPoint:point toView:self.centerBtn];
        
        //判断如果这个新的点是在发布按钮身上，那么处理点击事件最合适的view就是发布按钮
        if ( [self.centerBtn pointInside:newP withEvent:event]) {
            return self.centerBtn;
        }else{//如果点不在发布按钮身上，直接让系统处理就可以了
            
            return [super hitTest:point withEvent:event];
        }
    }
    
    else {//tabbar隐藏了，那么说明已经push到其他的页面了，这个时候还是让系统去判断最合适的view处理就好了
        
        return [super hitTest:point withEvent:event];
    }
}
   //设置中心按钮的点击状态默认为选中状态
- (void)setCenterButtonSelected:(BOOL)selected {
    _centerBtn.selected = selected;
}

@end



@implementation CenterButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setSubviews];
    }
    return self;
}

- (void)setSubviews
{
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
//    [self addGestureRecognizer:tap];
//
//    UIView *maskView = [[UIView alloc] init];
//    maskView.frame = CGRectMake(0, 0, CenterBtnW, topMargin);
//    maskView.layer.masksToBounds = YES;
//    [self addSubview:maskView];
//
//    UIView *backView = [[UIView alloc] init];
//    backView.size = CGSizeMake(CenterBtnW, CenterBtnW);
//    backView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.8];
//    backView.layer.cornerRadius = CenterBtnW * 0.5;
//    backView.layer.masksToBounds = YES;
//    [maskView addSubview:backView];
//
//    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
//    UIVisualEffectView *blurView = [[UIVisualEffectView alloc] initWithEffect:blur];
//    blurView.frame = CGRectMake(0, 0, CenterBtnW, CenterBtnW);
//    blurView.layer.cornerRadius = CenterBtnW * 0.5;
//    blurView.layer.borderColor = kColor_word_gray_2.CGColor;
//    blurView.layer.borderWidth = 0.5;
//    blurView.layer.masksToBounds = YES;
//    [maskView addSubview:blurView];
//
//    UIImageView *imageView = [[UIImageView alloc] init];
//    imageView.userInteractionEnabled = YES;
//    imageView.size = CGSizeMake(CenterBtnW - 10, CenterBtnW - 10);
//    imageView.cenX = CenterBtnW * 0.5;
//    imageView.cenY = CenterBtnW * 0.5;
//    imageView.image = [UIImage imageNamed:@"sell_icon"];
//    imageView.contentMode = UIViewContentModeScaleAspectFit;
//    [self addSubview:imageView];
}

- (void)onTap:(UITapGestureRecognizer *)tap
{
    self.centerBtnblock ? self.centerBtnblock() : nil;
}

@end
